﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using QuanLyRapChieuPhim.DTO;
using System.ComponentModel;

namespace QuanLyRapChieuPhim.DAO
{
    class PhongChieuDAO
    {
        DataProvider dp = new DataProvider();

        public BindingList<PhongChieuDTO> GetList()
        {

            DataTable dt = new DataTable();
            BindingList<PhongChieuDTO> ds = new BindingList<PhongChieuDTO>();
            if (dp.OpenConnect())
            {
                dt = dp.SqlExcuteQuery("uproc_GetList_PhongChieu", null);
                foreach (DataRow row in dt.Rows)
                {
                    PhongChieuDTO PhongChieu = new PhongChieuDTO();
                    PhongChieu.MaPhongChieu = int.Parse(row["maPhongChieu"].ToString());
                    PhongChieu.MaRap = int.Parse(row["maRap"].ToString());
                    PhongChieu.SoHangGhe = int.Parse(row["soHangGhe"].ToString());
                    PhongChieu.SoDayGhe = int.Parse(row["soDayGhe"].ToString());
                    PhongChieu.MoTa = row["moTa"].ToString();
                    PhongChieu.TenPhongChieu = row["tenPhongChieu"].ToString();
                    PhongChieu.MaLoaiKyThuat = int.Parse(row["maLoaiKyThuat"].ToString());
                    PhongChieu.MaTinhTrangPhongChieu = int.Parse(row["maTinhTrang"].ToString());
                    ds.Add(PhongChieu);
                }
                dp.CloseConnect();
                return ds;
            }
            return null;
        }

        public BindingList<PhongChieuDTO> GetList_QL()
        {

            DataTable dt = new DataTable();
            BindingList<PhongChieuDTO> ds = new BindingList<PhongChieuDTO>();
            if (dp.OpenConnect())
            {
                dt = dp.SqlExcuteQuery("uproc_GetList_PhongChieu_QL", null);
                foreach (DataRow row in dt.Rows)
                {
                    PhongChieuDTO PhongChieu = new PhongChieuDTO();
                    PhongChieu.MaLoaiKyThuat = int.Parse(row["maLoaiKyThuat"].ToString());
                    PhongChieu.MaPhongChieu = int.Parse(row["maPhongChieu"].ToString());
                    PhongChieu.MaRap = int.Parse(row["maRap"].ToString());
                    PhongChieu.MaTinhTrangPhongChieu = int.Parse(row["maTinhTrang"].ToString());
                    PhongChieu.MoTa = row["moTa"].ToString();
                    PhongChieu.TenPhongChieu = row["tenPhongChieu"].ToString();
                    PhongChieu.SoDayGhe = int.Parse(row["soDayGhe"].ToString());
                    PhongChieu.SoHangGhe = int.Parse(row["soHangGhe"].ToString());
                    PhongChieu.TenRap = row["tenRap"].ToString();
                    PhongChieu.TenTinhTrangPhongChieu = row["tenTinhTrangPhongChieu"].ToString();
                    PhongChieu.TenLoaiKyThuat = row["tenLoaiKyThuat"].ToString();
                    PhongChieu.DaXoa = int.Parse(row["DaXoa"].ToString());
                    ds.Add(PhongChieu);
                }
                dp.CloseConnect();
                return ds;
            }
            return null;
        }

        public BindingList<PhongChieuDTO> GetList_Rap(int _Ma)
        {

            DataTable dt = new DataTable();
            BindingList<PhongChieuDTO> ds = new BindingList<PhongChieuDTO>();
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = { new SqlParameter("@MaRap", _Ma) };
                dt = dp.SqlExcuteQuery("uproc_GetList_PhongChieu_Rap", sqlParameter);
                foreach (DataRow row in dt.Rows)
                {
                    PhongChieuDTO PhongChieu = new PhongChieuDTO();
                    PhongChieu.MaLoaiKyThuat = int.Parse(row["maLoaiKyThuat"].ToString());
                    PhongChieu.MaPhongChieu = int.Parse(row["maPhongChieu"].ToString());
                    PhongChieu.MaRap = int.Parse(row["maRap"].ToString());
                    PhongChieu.MaTinhTrangPhongChieu = int.Parse(row["maTinhTrang"].ToString());
                    PhongChieu.MoTa = row["moTa"].ToString();
                    PhongChieu.TenPhongChieu = row["tenPhongChieu"].ToString();
                    PhongChieu.SoDayGhe = int.Parse(row["soDayGhe"].ToString());
                    PhongChieu.SoHangGhe = int.Parse(row["soHangGhe"].ToString());
                    PhongChieu.TenRap = row["tenRap"].ToString();
                    PhongChieu.TenTinhTrangPhongChieu = row["tenTinhTrangPhongChieu"].ToString();
                    PhongChieu.TenLoaiKyThuat = row["tenLoaiKyThuat"].ToString();
                    PhongChieu.DaXoa = int.Parse(row["DaXoa"].ToString());
                    ds.Add(PhongChieu);
                }
                dp.CloseConnect();
                return ds;
            }
            return null;
        }

        public BindingList<PhongChieuDTO> GetList_QL_DaXoa()
        {

            DataTable dt = new DataTable();
            BindingList<PhongChieuDTO> ds = new BindingList<PhongChieuDTO>();
            if (dp.OpenConnect())
            {
                dt = dp.SqlExcuteQuery("uproc_GetList_PhongChieu_QL_DaXoa", null);
                foreach (DataRow row in dt.Rows)
                {
                    PhongChieuDTO PhongChieu = new PhongChieuDTO();
                    PhongChieu.MaLoaiKyThuat = int.Parse(row["maLoaiKyThuat"].ToString());
                    PhongChieu.MaPhongChieu = int.Parse(row["maPhongChieu"].ToString());
                    PhongChieu.MaRap = int.Parse(row["maRap"].ToString());
                    PhongChieu.MaTinhTrangPhongChieu = int.Parse(row["maTinhTrang"].ToString());
                    PhongChieu.MoTa = row["moTa"].ToString();
                    PhongChieu.TenPhongChieu = row["tenPhongChieu"].ToString();
                    PhongChieu.SoDayGhe = int.Parse(row["soDayGhe"].ToString());
                    PhongChieu.SoHangGhe = int.Parse(row["soHangGhe"].ToString());
                    PhongChieu.TenRap = row["tenRap"].ToString();
                    PhongChieu.TenTinhTrangPhongChieu = row["tenTinhTrangPhongChieu"].ToString();
                    PhongChieu.TenLoaiKyThuat = row["tenLoaiKyThuat"].ToString();
                    PhongChieu.DaXoa = int.Parse(row["DaXoa"].ToString());
                    ds.Add(PhongChieu);
                }
                dp.CloseConnect();
                return ds;
            }
            return null;
        }

        public BindingList<PhongChieuDTO> GetListLikeName_QL(String _Ten)
        {

            DataTable dt = new DataTable();
            BindingList<PhongChieuDTO> ds = new BindingList<PhongChieuDTO>();
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@Ten", _Ten) 
                };
                dt = dp.SqlExcuteQuery("uproc_GetListLikeName_PhongChieu_QL", sqlParameter);
                foreach (DataRow row in dt.Rows)
                {
                    PhongChieuDTO PhongChieu = new PhongChieuDTO();
                    PhongChieu.MaLoaiKyThuat = int.Parse(row["maLoaiKyThuat"].ToString());
                    PhongChieu.MaPhongChieu = int.Parse(row["maPhongChieu"].ToString());
                    PhongChieu.MaRap = int.Parse(row["maRap"].ToString());
                    PhongChieu.MaTinhTrangPhongChieu = int.Parse(row["maTinhTrang"].ToString());
                    PhongChieu.MoTa = row["moTa"].ToString();
                    PhongChieu.TenPhongChieu = row["tenPhongChieu"].ToString();
                    PhongChieu.SoDayGhe = int.Parse(row["soDayGhe"].ToString());
                    PhongChieu.SoHangGhe = int.Parse(row["soHangGhe"].ToString());
                    PhongChieu.TenRap = row["tenRap"].ToString();
                    PhongChieu.TenTinhTrangPhongChieu = row["tenTinhTrangPhongChieu"].ToString();
                    PhongChieu.TenLoaiKyThuat = row["tenLoaiKyThuat"].ToString();
                    PhongChieu.DaXoa = int.Parse(row["DaXoa"].ToString());
                    ds.Add(PhongChieu);
                }
                dp.CloseConnect();
                return ds;
            }
            return null;
        }

        public BindingList<PhongChieuDTO> GetListLikeName_QL_DaXoa(String _Ten)
        {

            DataTable dt = new DataTable();
            BindingList<PhongChieuDTO> ds = new BindingList<PhongChieuDTO>();
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@Ten", _Ten) 
                };
                dt = dp.SqlExcuteQuery("uproc_GetListLikeName_PhongChieu_QL_DaXoa", sqlParameter);
                foreach (DataRow row in dt.Rows)
                {
                    PhongChieuDTO PhongChieu = new PhongChieuDTO();
                    PhongChieu.MaLoaiKyThuat = int.Parse(row["maLoaiKyThuat"].ToString());
                    PhongChieu.MaPhongChieu = int.Parse(row["maPhongChieu"].ToString());
                    PhongChieu.MaRap = int.Parse(row["maRap"].ToString());
                    PhongChieu.MaTinhTrangPhongChieu = int.Parse(row["maTinhTrang"].ToString());
                    PhongChieu.MoTa = row["moTa"].ToString();
                    PhongChieu.TenPhongChieu = row["tenPhongChieu"].ToString();
                    PhongChieu.SoDayGhe = int.Parse(row["soDayGhe"].ToString());
                    PhongChieu.SoHangGhe = int.Parse(row["soHangGhe"].ToString());
                    PhongChieu.TenRap = row["tenRap"].ToString();
                    PhongChieu.TenTinhTrangPhongChieu = row["tenTinhTrangPhongChieu"].ToString();
                    PhongChieu.TenLoaiKyThuat = row["tenLoaiKyThuat"].ToString();
                    PhongChieu.DaXoa = int.Parse(row["DaXoa"].ToString());
                    ds.Add(PhongChieu);
                }
                dp.CloseConnect();
                return ds;
            }
            return null;
        }

        public PhongChieuDTO GetFromId(int _Ma)
        {
            DataTable dt = new DataTable();
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = { new SqlParameter("@Ma", _Ma) };
                dt = dp.SqlExcuteQuery("uproc_GetFromID_PhongChieu", sqlParameter);
                PhongChieuDTO PhongChieu = new PhongChieuDTO();
                foreach (DataRow row in dt.Rows)
                {
                    PhongChieu.MaPhongChieu = int.Parse(row["maPhongChieu"].ToString());
                    PhongChieu.MaRap = int.Parse(row["maRap"].ToString());
                    PhongChieu.SoHangGhe = int.Parse(row["soHangGhe"].ToString());
                    PhongChieu.SoDayGhe = int.Parse(row["soDayGhe"].ToString());
                    PhongChieu.MoTa = row["moTa"].ToString();
                    PhongChieu.TenPhongChieu = row["tenPhongChieu"].ToString();
                    PhongChieu.MaLoaiKyThuat = int.Parse(row["maLoaiKyThuat"].ToString());
                    PhongChieu.MaTinhTrangPhongChieu = int.Parse(row["maTinhTrang"].ToString());
                }
                dp.CloseConnect();
                return PhongChieu;
            }
            return null;
        }

        public int IdRapFromID(int _Ma)
        {
            DataTable dt = new DataTable();
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = { new SqlParameter("@maPhongChieu", _Ma) };
                dt = dp.SqlExcuteQuery("uproc_IdRapFromIDPhongChieu", sqlParameter);
                dp.CloseConnect();
                if (dt.Rows.Count > 0)
                {
                    int kq = int.Parse(dt.Rows[0][0].ToString());
                    return kq;
                }
                return 0;
            }
            return -1;
        }

        public bool Delete(int _Ma)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@Ma", _Ma) 
                };
                bool kq = dp.SqlExcuteNonQuery("uproc_Delete_PhongChieu", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;
        }

        public bool UnDelete(int _Ma)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@Ma", _Ma) 
                };
                bool kq = dp.SqlExcuteNonQuery("uproc_UnDelete_PhongChieu", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;
        }

        public bool Add(PhongChieuDTO _DTO)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@TenPhongChieu", _DTO.TenPhongChieu),
                    new SqlParameter("@MaLoaiKyThuat", _DTO.MaLoaiKyThuat),
                    new SqlParameter("@MaRap", _DTO.MaRap), 
                    new SqlParameter("@MaTinhTrang", _DTO.MaTinhTrangPhongChieu),
                    new SqlParameter("@MoTa", _DTO.MoTa),
                    new SqlParameter("@SoDayGhe", _DTO.SoDayGhe),
                    new SqlParameter("@SoHangGhe", _DTO.SoHangGhe)
                };
                bool kq = dp.SqlExcuteNonQuery("uproc_Add_PhongChieu", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;

        }

        public bool CheckExists(int _Ma, string _Ten)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@Ma", _Ma), 
                    new SqlParameter("@TenPhongChieu", _Ten)
                };
                DataTable dt = dp.SqlExcuteQuery("uproc_CheckExists_PhongChieu", sqlParameter);
                dp.CloseConnect();
                if (dt.Rows.Count == 0)
                    return false;
                return true;// đã tồn tại
            }
            return false;
        }

        public bool Update(PhongChieuDTO _DTO)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@MaPhongChieu", _DTO.MaPhongChieu),
                    new SqlParameter("@TenPhongChieu", _DTO.TenPhongChieu),
                    new SqlParameter("@MaLoaiKyThuat", _DTO.MaLoaiKyThuat),
                    new SqlParameter("@MaRap", _DTO.MaRap), 
                    new SqlParameter("@MaTinhTrang", _DTO.MaTinhTrangPhongChieu),
                    new SqlParameter("@MoTa", _DTO.MoTa),
                    new SqlParameter("@SoDayGhe", _DTO.SoDayGhe),
                    new SqlParameter("@SoHangGhe", _DTO.SoHangGhe)
                };
                bool kq = dp.SqlExcuteNonQuery("uproc_Update_PhongChieu", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;
        }
    }
}
